package com.shenhonghui.photo.slice;

import com.shenhonghui.photo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.ability.DataAbilityRemoteException;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.bundle.AbilityInfo;
import ohos.data.resultset.ResultSet;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.Size;
import ohos.media.photokit.metadata.AVStorage;
import ohos.media.photokit.metadata.AVThumbnailUtils;
import ohos.utils.net.Uri;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileNotFoundException;
import java.net.URI;

public class PictureSetAbilitySlice extends AbilitySlice {
    private TableLayout tableLayout;
    private Text title;
    private Image photo;
    private ScrollView scrollView;
    private StackLayout stackLayout;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_photos);
        setDisplayOrientation(AbilityInfo.DisplayOrientation.PORTRAIT);
        title = (Text)findComponentById(ResourceTable.Id_title);
        photo = (Image)findComponentById(ResourceTable.Id_photo);
        tableLayout = (TableLayout)findComponentById(ResourceTable.Id_picture_set);
        stackLayout = (StackLayout) findComponentById(ResourceTable.Id_stackLayout);
        scrollView = (ScrollView)findComponentById(ResourceTable.Id_scrollView);
        tableLayout.setColumnCount(4);
        showImage();
        Image backImage = (Image)findComponentById(ResourceTable.Id_back);
        backImage.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                onBackPressed();
            }
        });

        photo.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                scrollView.setVisibility(Component.VISIBLE);
                stackLayout.moveChildToFront(scrollView);
                photo.setVisibility(Component.HIDE);
                title.setText("照片");
            }
        });

        scrollView.setScrolledListener((component, i, i1, i2, i3) -> {
            scrollView.fluentScrollByY(i1 - i3);
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onBackPressed() {
        super.onBackPressed();
    }

    private void showImage() {
        DataAbilityHelper helper = DataAbilityHelper.creator(this);
        try {
            // columns为null，查询记录所有字段，当前例子表示查询id字段
            ResultSet resultSet = helper.query(AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI, new String[]{AVStorage.Images.Media.ID}, null);
            if(resultSet != null ) {
                resultSet.goToLastRow();
                PixelMap pixelMap = null;
                ImageSource imageSource = null;
                ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
                decodingOptions.rotateDegrees = 90;
                do {
                    Image image = new Image(this);
                    image.setWidth(250);
                    image.setHeight(250);
                    image.setMarginsLeftAndRight(10, 10);
                    image.setMarginsTopAndBottom(10, 10);
                    image.setScaleMode(Image.ScaleMode.CLIP_CENTER);
                    // 获取id字段的值
                    int id = resultSet.getInt(resultSet.getColumnIndexForName(AVStorage.Images.Media.ID));
                    Uri uri = Uri.appendEncodedPathToUri(AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI, String.valueOf(id));
                    FileDescriptor fd = helper.openFile(uri, "r");
                    try {
                        imageSource = ImageSource.create(fd, null);
                        pixelMap = imageSource.createPixelmap(decodingOptions);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    image.setPixelMap(pixelMap);

                    image.setClickedListener(new Component.ClickedListener() {
                        @Override
                        public void onClick(Component component) {
                            title.setText(String.valueOf(id));
                            photo.setPixelMap(image.getPixelMap());
                            photo.setScaleMode(Image.ScaleMode.ZOOM_CENTER);
                            photo.setVisibility(Component.VISIBLE);
                            stackLayout.moveChildToFront(photo);
                            scrollView.setVisibility(Component.HIDE);
                        }
                    });
                    tableLayout.addComponent(image);
                }while (resultSet.goToPreviousRow());
                imageSource.release();
                pixelMap.release();
            }
        } catch (DataAbilityRemoteException | FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
